//
//  InfoViewController.m
//  groupProject
//
//  Created by mac on 15/12/17.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "InfoViewController.h"
#import "Config.h"
#import "CollectViewController.h"
#import "HisoryViewController.h"
#import "PassLoginView.h"
#import "FunRectView.h"
#import "OrderInformationViewController.h"
#import "VIpViewController.h"
#import "FeedbackViewController.h"
#import "CallCenterViewController.h"
@interface InfoViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation InfoViewController

{
    UIButton * PersonHeard; // 头像按钮
    NSArray * dataArray;
    NSArray * CollectButtonArray;
    NSArray * CollectLabelArray;
    UILabel * labelName;
    UILabel * LevelLabel;
    NSArray * CellImageArray;
    
    
    UILabel * allOrder;
    UILabel * SeeAllOrder;
    UIButton * myAllOrder;
    UIView *btnView;
}
@synthesize LoginTableView;
@synthesize CommodityCount,ShopCount,HistoryCount;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的信息";
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    LoginTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-49)];
    LoginTableView.tableHeaderView = [self heardView];
    LoginTableView.delegate = self;
    LoginTableView.dataSource = self;
    [self.view addSubview:LoginTableView];
    dataArray = [NSArray arrayWithObjects:@"会员中心",@"我客服中心",@"意见发馈", nil];
    CellImageArray =[NSArray arrayWithObjects:@"Cell_Vip.JPG", @"Cell_Mes.JPG",@"Cell_Adc.JPG",nil];
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithTitle:@"注销"style:UIBarButtonItemStylePlain target:self action:@selector(CanceToPass)];
    [leftBtn setTintColor:[UIColor redColor]];
    self.navigationItem.leftBarButtonItem = leftBtn;
    
}
-(UIView *)heardView{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT/4.0+20+SCREEN_WIDTH/8.0+SCREEN_WIDTH*0.2+10)];
    //100+14+SCREEN_WIDTH/9.0)
    UIView * view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/6.0)];
    view1.backgroundColor = [UIColor redColor];
    PersonHeard = [[UIButton alloc]initWithFrame:CGRectMake(20, 10, 80, 80)];
    [PersonHeard setBackgroundColor:[UIColor lightGrayColor]];
    [PersonHeard setTitle:@"添加头像" forState:UIControlStateNormal];
    [PersonHeard setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    // [PersonHeard setImage:[UIImage imageNamed:@"Person.jpg"] forState:UIControlStateNormal];
    PersonHeard.layer.cornerRadius = 40.0;
    PersonHeard.layer.borderWidth = 1.0;
    PersonHeard.layer.borderColor =[UIColor clearColor].CGColor;
    PersonHeard.clipsToBounds = TRUE;//去除边界
    [PersonHeard addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:PersonHeard];
    //用户名文字
    labelName= [[UILabel alloc]initWithFrame:CGRectMake(110, 20, 150, 20)];
    labelName.textColor = [UIColor whiteColor];
    [view1 addSubview:labelName];
    //用户等级
    LevelLabel = [[UILabel alloc]initWithFrame:CGRectMake(110, 41, 100, 20)];
    
    [view1 addSubview:LevelLabel];
    
    [self AddCollectLabel];
    [self AddCollectButton];
    for (int i = 0; i<3; i++) {
        [view addSubview:CollectLabelArray[i]];
        [view addSubview:CollectButtonArray[i]];
    }
    [view addSubview:view1];
    [self addOrderInformation];
    [view addSubview:allOrder];
    [view addSubview:SeeAllOrder];
    [view addSubview:myAllOrder];
    [view addSubview:btnView];
    return  view;
}
//添加收藏按钮以及label
-(void)AddCollectLabel{
    //收藏商品个数 初始为0
    CommodityCount = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/6.0-5, SCREEN_HEIGHT/6.0+5, 20, 14)];
    CommodityCount.text = @"0";
    CommodityCount.textColor = [UIColor lightGrayColor];
    //收藏店铺个数 初始为0
    ShopCount = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/6.0*3-5, SCREEN_HEIGHT/6.0+5, 20, 14)];
    ShopCount.text = @"0";
    ShopCount.textColor = [UIColor lightGrayColor];
    //浏览足迹个数 初始为0
    HistoryCount = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/6.0*5-5, SCREEN_HEIGHT/6.0+5, 20, 14)];
    HistoryCount.text = @"0";
    HistoryCount.textColor = [UIColor lightGrayColor];
    
    CollectLabelArray = [NSArray arrayWithObjects:CommodityCount,ShopCount,HistoryCount, nil];
    
}
//添加收藏按钮以及label
-(void)AddCollectButton{
    //收藏的商品
    UIButton * button1 = [[UIButton alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT/6.0+20,SCREEN_WIDTH/3.0, SCREEN_WIDTH/9.0)];
    // button1.backgroundColor =[UIColor redColor];
    [button1 setTitle:@"收藏的商品" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    button1.tag = 1;
    [button1 addTarget:self action:@selector(CollectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //收藏的店铺
    UIButton * button2 = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/3.0, SCREEN_HEIGHT/6.0+20, SCREEN_WIDTH/3.0, SCREEN_WIDTH/9.0)];
    [button2 setTitle:@"收藏的店铺" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(CollectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //购物足迹
    UIButton * button3 = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/3.0 *2,SCREEN_HEIGHT/6.0+20, SCREEN_WIDTH/3.0, SCREEN_WIDTH/9.0)];
    [button3 setTitle:@"购物足迹" forState:UIControlStateNormal];
    [button3 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(HistoryBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    CollectButtonArray = [NSArray arrayWithObjects:button1,button2,button3, nil];
}
-(void)addOrderInformation{
    allOrder = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT/6.0+25+SCREEN_WIDTH/9.0, SCREEN_WIDTH/3.0, SCREEN_WIDTH/8.0)];
    allOrder.text = @"我的订单";
    allOrder.textColor = [UIColor blackColor];
    SeeAllOrder = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/3.0*2, SCREEN_HEIGHT/6.0+25+SCREEN_WIDTH/9.0, SCREEN_WIDTH/3.0, SCREEN_WIDTH/8.0)];
    SeeAllOrder.text = @"查看全部订单 》";
    SeeAllOrder.textColor = [UIColor blackColor];
    
    myAllOrder = [[UIButton alloc]initWithFrame:CGRectMake(0,  SCREEN_HEIGHT/6.0+25+SCREEN_WIDTH/9.0, SCREEN_WIDTH, SCREEN_WIDTH/8.0)];
    [myAllOrder addTarget:self action:@selector(myAllOrderClick) forControlEvents:UIControlEventTouchUpInside];
    
    //btn 区
    NSArray *titleArray = [NSArray arrayWithObjects:@"待付款", @"待发货",@"待收货",@"待评价", @"退换货",nil];
    btnView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT/6.0+30+SCREEN_WIDTH/9.0+SCREEN_WIDTH/8.0, SCREEN_WIDTH, SCREEN_WIDTH*0.2)];
    btnView.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
    for (int i = 0; i< 5; i++) {
        FunRectView *funView = [[FunRectView alloc] initWithFrame:CGRectMake(i*SCREEN_WIDTH/5.0, 0, SCREEN_WIDTH/5.0, SCREEN_WIDTH *0.2) andImage:[NSString stringWithFormat:@"I%d.jpg",i+1] andTitle:titleArray[i]];
        funView.btntag = i;
        [funView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(funBiewClick:)]];
        [btnView addSubview:funView];
    }
}
-(void)funBiewClick:(UITapGestureRecognizer *)sender{
    FunRectView *tmp =(FunRectView *) [sender view];
    OrderInformationViewController * vc = [[OrderInformationViewController alloc]init];
    if (tmp.btntag ==0) {
        vc.segmentIndex = 1;
    }else if (tmp.btntag ==1){
        vc.segmentIndex = 2;
    }else if (tmp.btntag ==2){
        vc.segmentIndex = 3;
    }else if (tmp.btntag ==3){
        vc.segmentIndex = 4;
    }else if (tmp.btntag ==4){
        vc.segmentIndex = 5;
    }
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)myAllOrderClick{
    NSLog(@"myAllOrderClick");
    OrderInformationViewController * vc = [[OrderInformationViewController alloc]init];
    vc.segmentIndex = 0;
    [self.navigationController pushViewController:vc animated:YES];
}
//    *******收藏夹*******
-(void)CollectBtnClick:(UIButton *)sender{
    
    CollectViewController * VC = [[CollectViewController alloc]init];
    VC.segmentIndex = (int)sender.tag;
    [self.navigationController pushViewController:VC animated:YES];
    
}

//注销并进入登录界面
-(void)CanceToPass{
    //   UIAlertView * alertView = [UIAlertView]
    UIAlertController * alertView = [UIAlertController alertControllerWithTitle:@"注销" message:@"您真的要离开吗？在看看吧!" preferredStyle:UIAlertControllerStyleAlert];
    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *CertainAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"确定");
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        [userDefault removeObjectForKey:@"username"];
        [userDefault removeObjectForKey:@"password"];
        [userDefault removeObjectForKey:@"level1"];
        
        PassLoginView  * Pvc = [[PassLoginView alloc]init];
        [self.navigationController pushViewController:Pvc animated:YES];
        
    }];
    
    // Add the actions.
    [alertView addAction:cancelAction];
    [alertView addAction:CertainAction];
    [self presentViewController:alertView animated:YES completion:nil];
}

//    *******历史记录*******
-(void)HistoryBtnClick{
    HisoryViewController * HVC = [[HisoryViewController alloc]init];
    [self.navigationController pushViewController:HVC animated:YES];
    
}
//  iOS弹出的选择按钮项，可以添加多项，并为每项添加点击事件
-(void)btnClick{
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil                                                                             message: nil                                                                       preferredStyle:UIAlertControllerStyleActionSheet];
    //添加Button
    [alertController addAction: [UIAlertAction actionWithTitle: @"拍照" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        //处理点击拍照
        [self takePhoto];
        
    }]];
    [alertController addAction: [UIAlertAction actionWithTitle: @"从相册选取" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        //处理点击从相册选取
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        imagePicker.delegate = self;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }]];
    [alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]];
    
    [self presentViewController: alertController animated: YES completion: nil];
    
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    [PersonHeard setImage:image forState:UIControlStateNormal];
    [self dismissViewControllerAnimated:YES completion:nil];
}
//打开照相机
-(void)takePhoto{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        //[self presentModalViewController:picker animated:YES];
        [self presentViewController: picker animated: YES completion: nil];
    }else
    {
        NSLog(@"模拟其中无法打开照相机,请在真机中使用");
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *resuleID = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resuleID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:resuleID];
    }
    NSString *tmp = dataArray[indexPath.row];
    NSString * tmpImageName = CellImageArray[indexPath.row];
    cell.textLabel.text = tmp;
    cell.imageView.image = [UIImage imageNamed:tmpImageName];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        NSLog(@"会员");
        VIpViewController * VIp = [[VIpViewController alloc]init];
        [self.navigationController pushViewController:VIp animated:YES];
    }
    if (indexPath.row==1) {
        CallCenterViewController * Call = [[CallCenterViewController alloc]init];
        [self.navigationController pushViewController:Call animated:YES];
        NSLog(@"客服");
    }
    if (indexPath.row==2) {
        NSLog(@"意见");
        FeedbackViewController * Feed = [[FeedbackViewController alloc]init];
        [self.navigationController pushViewController:Feed animated:YES];
    }
    
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated {
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *name = [userDefault objectForKey:@"username"];
    NSString *password = [userDefault objectForKey:@"password"];
    NSNumber * level =[userDefault objectForKey:@"level1"];
    if ( name == nil && password ==nil) {
        NSLog(@"123sss");
        PassLoginView * PVC = [[PassLoginView alloc]init];
        [self.navigationController pushViewController:PVC animated:NO];
    }
    labelName.text = name;
    LevelLabel.text = [NSString stringWithFormat:@"V%@会员",level];
    self.tabBarController.tabBar.hidden = NO;
}






/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
